﻿#ifndef COMMON_H
#define COMMON_H
#include <QDebug>
#include <QThread>
#include "halconcpp/HalconCpp.h"
#include "ui_mainwindow.h"
#include <string.h>
#include "Alg/Defect.h"
#include "Alg/Measure_Rect.h"
#include "Alg/Judge_Direction.h"
#include "Alg/Read_Model.h"
#include "Alg/Create_Model.h"
#include "Alg/Measure_Trapezoid.h"
#include "Alg/Measure_TS.h"
#include "Alg/Measure_TSS.h"
#include "Alg/Create_Model.h"
#include "Alg/Judge_Materiel.h"
#include "Alg/Measure_Side.h"
#include "Alg/Measure_Sector.h"
#include "Alg/Measure_Bullet.h"


#include "Alg/Defect_Rect.h"
#include "Alg/Defect_Sector.h"
#include "Alg/Defect_Side.h"
#include "Alg/Defect_Fback.h"



const int PLCSUCCEED = 0;
const int PLCDBNUMBER = 3;
const int PLCRACK = 0;
const int PLCSLOT = 1;

namespace Ui {
class MainWindow;
//class PlcDlg;
}
// plc 信号值
enum SIG_PLC {
    START,//PC_开始
    STOP,  //PC_停止
    MOTOR1, //PC_玻璃盘1转动
    MOTOR2, //PC_上料2转动
    INCODERA,// 编码器A
    INCODERB, // 编码器B
    INSENSOR, //光纤传感器
    INPRESSSW1,//压力开关
    INPAUSESW,//暂停按钮
    QPULSE1,  //玻璃盘电机1脉冲
    QDIRECTION1,//玻璃盘电机1方向
    QPULSE2,//上料电机2脉冲
    QDIRECTION2,//上料电机2方向
    QENABLED1,//玻璃盘电机1使能
    QENABLED2,//上料电机2使能
    QGRAP1,//相机1拍照
    QGRAP2,//相机2拍照
    QGRAP3,//相机3拍照
    QGRAP4,//相机4拍照
    QGRAP5,//相机5拍照
    QGRAP6,//相机6拍照
    QGRAP7,//相机7拍照
    QGRAP8,//相机8拍照
    QNG1OUTAIR,//高速电磁阀1
    QNG2OUTAIR,//高速电磁阀2
    QNG3OUTAIR,//高速电磁阀3
    QNG4OUTAIR,//高速电磁阀4
    QNG5OUTAIR,//高速电磁阀5
    QNG6OUTAIR,//高速电磁阀6
    QPLATE,//震动盘
    QINAIR,//系统上气
    QLINE,//照明灯
    QTIMEOUT_LINE,//暂停灯
    QLIGHTG,//三色灯-绿
    QLIGHTY,//三色灯-黄
    QLIGHTR,//三色灯-红
    QLIGHTVOX,//三色灯-鸣
    MOTOR1_REV,//PC_玻璃盘电机转速
    MOTOR2_REV,//PC_上料电机转速
    MOTOR_RATION1,//PC_玻璃盘电机转比
    MOTOR_RATION2,//PC_上料电机转比
    OPTICAL,//PC_光纤位置
    PCCD_1,//PC_相机1距离
    PCCD_2,//PC_相机2距离
    PCCD_3,//PC_相机3距离
    PCCD_4,//PC_相机4距离
    PCCD_5,//PC_相机5距离
    PCCD_6,//PC_相机6距离
    PCCD_7,//PC_相机7距离
    PCCD_8,//PC_相机8距离
    MOTOR_NG1,//真空距离
    MOTOR_NG2,//倒料距离
    MOTOR_NG3,//NG1距离
    MOTOR_NG4,//NG2距离
    MOTOR_NG5,//NG3距离
    MOTOR_NG6,//PC_OK距离
    RESULT_NUMBER,//PC_料号
    CAMERA_RESULT, //PC_结果
    WARN_TIME,//PC_缺料报警时间
    STOP_TIME,//PC_停机延迟时间
    ENCODER, //编码器值
    BLOW_TIME,//吹料时间
    FALL_TIME, //吹倒料时间
    CUT_STATE,  //切换PLC 状态
    TOUCH_CCD1A, //相机1触发A
    TOUCH_CCD1B, //相机1触发B
    TOUCH_CCD2A, //相机2触发A
    TOUCH_CCD2B, //相机2触发B
    TOUCH_CCD3A, //相机3触发A
    TOUCH_CCD3B, //相机3触发B
    TOUCH_CCD4A, //相机4触发A
    TOUCH_CCD4B, //相机4触发B
    TOUCH_CCD5A, //相机5触发A
    TOUCH_CCD5B, //相机5触发B
    TOUCH_CCD6A, //相机6触发A
    TOUCH_CCD6B  //相机6触发B
};

struct CameraData
{
    int m_nIndex;                           //编号
    QString qStrCameraID;
    HalconCpp::HTuple hv_WindowHandle;
    HalconCpp::HObject Hobj;
    int m_nNGorOK;

};
struct CameraInfo
{
    int checkSum = 0;
    int goodSum = 0;
    int badSum = 0;
};

// PLC PLCdlg
struct PlcInfo{
    int nAddress = 0;
    int nValue = 0;
};

// Caemra
struct CameraParameter
{
    double ExposureTimeAbs;
    int OffsetX;
    int OffsetY;
    int Height;
    int Width;
};

struct CameraDlgInfo
{
    int m_nFunction;                    // 0.无 1.打开相机获取相机值 2.启动硬触发 3.启动连续采集 4.保存相机数据
    int m_nCamera;                      // 相机编号
    HalconCpp::HTuple hv_WindowHandle;  // 绑定的 Halcon 控件值
    bool m_bOpen;                       // 是否打开 Halcon 窗口
    int m_nSingle;                      // 单张硬触发
    int m_nConsecutive;                 // 连续采集
    int m_nPlate;                       // 是否启动转盘
    CameraParameter m_nParameter;       // 相机参数
};

// 存图   0  不存图   1 存图
struct SW_SaveImg{
    int nSwPic = 0;
    int nSwNg = 0;
    int nCCD1 = 0;
    int nCCD2 = 0;
    int nCCD3 = 0;
    int nCCD4 = 0;
    int nCCD5 = 0;
    int nCCD6 = 0;
    int nCCD7 = 0;
    int nCCD8 = 0;
};

// 测量   0 不测量    1 测量
struct SW_Check{
    int nCCD1 = 1;
    int nCCD2 = 1;
    int nCCD3 = 1;
    int nCCD4 = 1;
    int nCCD5 = 1;
    int nCCD6 = 1;
    int nCCD7 = 1;
    int nCCD8 = 1;
};


// 0 不选择 1 选择
struct SW_AlgPart
{
    int nCCD = 0;
    int nAlgA = 0;
    int nAlgB = 0;
    int nDefect = 0;
    int nRect = 0;
};

// 缺陷类型
struct SW_Alg
{
    SW_AlgPart m_ccd1;
    SW_AlgPart m_ccd2;
    SW_AlgPart m_ccd3;
    SW_AlgPart m_ccd4;
    SW_AlgPart m_ccd5;
    SW_AlgPart m_ccd6;
    SW_AlgPart m_ccd7;
    SW_AlgPart m_ccd8;
};
// 宽度  长度   角度1 ~ 4

struct M_R_DataA{
    float dWidth = 0;
    float dWidthPa = 0;
    float dLength = 0;
    float dLengthPa = 0;

    float dAngle1 = 0;
    float dAngle2 = 0;
    float dAngle3 = 0;
    float dAngle4 = 0;
};
// 厚度  厚度平行度
struct M_R_DataB{
    float dThickness = 0;
    float dThicknessPa = 0;
};

// 0 高度   1平行度  2 左斜边角度   3  右斜边角度
struct M_T_DataA
{
    float dHeight = 0;
    float dHeightPa = 0;
    float dLefthy = 0;
    float dRighthy = 0;
};
//  高度  宽度
struct M_TS_DataA
{
    float dHeight = 0;
    float dWidth = 0;
};
// 厚度
struct M_TSS_DataA
{
    float dThickness = 0;
};
// 弧边1长度  弧边2长度  直边3长度  直边4长度  中直长度 轮廓度
struct M_Se_DataA
{
    float dEdge1 = 0;
    float dEdge2 = 0;
    float dStraight3 = 0;
    float dStraight4 = 0;
    float dMStraight = 0;
    float dProfile = 0;
};

//宽度  长度  角度1  角度2
struct M_Bullet_DataA{
    float dWidth = 0;
    float dLength = 0;
    float dAngle1 = 0;
    float dAngle2 = 0;
};


// 缺陷
// 方形
struct D_R_DataA{
    float dInterior = 0;
    float dExternal = 0;
};

// 扇形
struct D_S_DataA{
    float dInterior = 0;
    float dExternal = 0;
};

// 计数
struct M_NUMBER
{
    M_R_DataA dM_R_DataA;
    M_R_DataB dM_R_DataB;
    M_T_DataA dM_T_DataA;
    M_TS_DataA dM_TS_DataA;
    M_TSS_DataA dM_TSS_DataA;
    M_Se_DataA dM_Se_DataA;
    M_Bullet_DataA dM_Bullet_DataA;
    D_R_DataA dD_R_DataA;
    D_S_DataA dD_S_DataA;
};


struct ALG_PLACE
{
    HalconCpp::HTuple x1 = 0;
    HalconCpp::HTuple y1 = 0;
    HalconCpp::HTuple x2 = 0;
    HalconCpp::HTuple y2 = 0;
};

struct CCD_ALG_PLACE
{
    ALG_PLACE cd1;
    ALG_PLACE cd2;
    ALG_PLACE cd3;
    ALG_PLACE cd4;
};
//检测开始时间,检测结束时间,产品类型,产品批次,产品名,产品规格,初始数量,结束数量,良品数,良品率,不良数,不良率,尺寸不合格数,缺陷不合格数,混料,异常
struct CsvData
{
    QString csStarTime;
    QString csStopTime;
    QString csType;
    QString csIndex;
    QString csName;
    QString csSize;
    QString csStarNum;
    QString csStopNum;
    QString csGodNum;
    QString csGodRatio;
    QString csBadNum;
    QString csBadRatio;
    QString csNG1;
    QString csNG2;
    QString csNG3;
    QString csNG4;

    QString csNum1;
    QString csNum2;
    QString csNum3;
    QString csNum4;
    QString csNum5;
    QString csNum6;
    QString csNum7;
    QString csNum8;
    QString csNum9;
    QString csNum10;
    QString csNum11;
    QString csNum12;

    QString csNum1Up;
    QString csNum1UpRatio;
    QString csNum1Down;
    QString csNum1DownRatio;
    QString csNum1Unknown;

    QString csNum2Up;
    QString csNum2UpRatio;
    QString csNum2Down;
    QString csNum2DownRatio;
    QString csNum2Unknown;

    QString csNum3Up;
    QString csNum3UpRatio;
    QString csNum3Down;
    QString csNum3DownRatio;
    QString csNum3Unknown;


    QString csNum4Up;
    QString csNum4UpRatio;
    QString csNum4Down;
    QString csNum4DownRatio;
    QString csNum4Unknown;

    QString csNum5Up;
    QString csNum5UpRatio;
    QString csNum5Down;
    QString csNum5DownRatio;
    QString csNum5Unknown;

    QString csNum6Up;
    QString csNum6UpRatio;
    QString csNum6Down;
    QString csNum6DownRatio;
    QString csNum6Unknown;



    QString csAngle1OK;
    QString csAngle1NG;
    QString csAngle1Up;
    QString csAngle1UpRatio;
    QString csAngle1Down;
    QString csAngle1DownRatio;
    QString csAngle1Unknown;

    QString csAngle2OK;
    QString csAngle2NG;
    QString csAngle2Up;
    QString csAngle2UpRatio;
    QString csAngle2Down;
    QString csAngle2DownRatio;
    QString csAngle2Unknown;


    QString csAngle3OK;
    QString csAngle3NG;
    QString csAngle3Up;
    QString csAngle3UpRatio;
    QString csAngle3Down;
    QString csAngle3DownRatio;
    QString csAngle3Unknown;

    QString csAngle4OK;
    QString csAngle4NG;
    QString csAngle4Up;
    QString csAngle4UpRatio;
    QString csAngle4Down;
    QString csAngle4DownRatio;
    QString csAngle4Unknown;

};

struct UserData
{
    int nId;
    QString strName;
    int nLevel;
};

#endif // COMMON_H
